package com.youdo.careyes.utils;

import android.os.Environment;
import android.util.Log;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SDCardUtils;
import com.blankj.utilcode.util.SPUtils;

import java.util.List;

/**
 * 常用工具
 *
 * @author ljb
 * @date 2025.07.25
 * @email 568838953@qq.com
 */

public class SpUtil {
    private final SPUtils sp = SPUtils.getInstance("udo");
    private static volatile SpUtil instance;
    private static final int[][] resolutions = {    // 分辨率
            {800, 600},    // SVGA
            {1280, 720},   // 720p
            {1920, 1080},  // 1080p
            {640, 480}     // VGA
    };

    public static SpUtil getInstance() {
        return LocalNetManagerHolder.INSTANCE;
    }

    private static class LocalNetManagerHolder {
        private static final SpUtil INSTANCE = new SpUtil();
    }

    public boolean getBoolean(@NonNull String key) {
        return sp.getBoolean(key, false);
    }

    public int getInt(@NonNull String key) {
        return sp.getInt(key, 0);
    }

    public void put(@NonNull String key, boolean value) {
        sp.put(key, value, false);
    }

    public void put(@NonNull String key, int value) {
        sp.put(key, value, false);
    }

    /**
     * 获取分辨率
     */
    public int[] getResolution() {
        int pos = AppUtil.constrain(sp.getInt(Constants.VIDEO_RESOLUTION), 0, 2);
        Log.e("VideoSet", "resize = " + resolutions[pos][0] + "," + resolutions[pos][1]);
        return resolutions[pos];
    }

    /**
     * 获取保存目录
     *
     * @return SD card:/storage/4312-0D15/
     */
    public String getSavePath() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Temp";
        int pos = AppUtil.constrain(sp.getInt(Constants.SELECT_CACHE_PATH), 0, 1);
        if (pos > 0) {
            List<String> pathList = SDCardUtils.getMountedSDCardPath();
            if (!pathList.isEmpty()) {
                if (pathList.size() > 1 && pos == 1){
                    path = pathList.get(1) + "/Temp";
                }else {
                    path = pathList.get(0) + "/Temp";
                }
            }
        }
        Log.e("VideoSet", "getSavePath = " + path + ",pos" + pos);
        return path;
    }
}
